import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AuthModule } from './auth/auth.module';
import { ConfigModule, ConfigService } from '@nestjs/config';
//引入数据库配置文件
import envConfig from '../config/env';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UserEntity } from '@/entity/user.entity';
import { RestaurantEntity } from '@/entity/restaurant.entity';
import { CategoryEntity } from '@/entity/category.entity';
import { DishEntity } from '@/entity/dish.entity';
import { OrderEntity } from '@/entity/order.entity';
import { PaymentEntity } from '@/entity/payment.entity';
import { RestaurantModule } from '@/modules/restaurant/restaurant.module';
import { OrderModule } from '@/modules/order/order.module';
import { PaymentModule } from '@/modules/payment/payment.module';
import { CategoryModule } from '@/modules/category/category.module';
import { UploadModule } from './modules/upload/upload.module';
import { UserUploadEntity } from '@/entity/user-upload.entity';
import { DishModule } from './modules/dish/dish.module';
@Module({
  imports: [
    // 设置为全局
    ConfigModule.forRoot({
      isGlobal: true, // 设置为全局
      envFilePath: [envConfig.path],
    }),
    // 引入数据库配置
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: async (configService: ConfigService) => ({
        type: 'mysql', // 数据库类型
        entities: [
          UserEntity,
          RestaurantEntity,
          CategoryEntity,
          DishEntity,
          OrderEntity,
          PaymentEntity,
          UserUploadEntity,
        ], // 数据表实体
        host: configService.get('DB_HOST', 'localhost'), // 主机，默认为localhost
        port: configService.get<number>('DB_PORT', 3306), // 端口号
        username: configService.get('DB_USER', 'root'), // 用户名
        password: configService.get('DB_PASSWORD', 'hj127666'), // 密码
        database: configService.get('DB_DATABASE', 'order'), //数据库名
        timezone: '+08:00', //服务器上配置的时区
        synchronize: true, //根据实体自动创建数据库表， 生产环境建议关闭
        autoLoadEntities: true, //自动加载实体
        dateStrings: true, //日期类型转换为字符串
        connectTimeout: 10000, //超时时间
        reconnectTries: 10, //重连次数
      }),
    }),
    AuthModule,
    RestaurantModule,
    OrderModule,
    PaymentModule,
    CategoryModule,
    UploadModule,
    DishModule,
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
